package transforms;

import java.util.Iterator;

import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

import com.rshacking.rhf.files.ClassContainer;
import com.rshacking.rhf.updater.Transform;
import com.rshacking.rhf.updater.hooks.ClassHook;
import com.rshacking.rhf.updater.hooks.HooksList;

public class PlayerTransforms extends Transform {

	@Override
	public boolean canActivate(final HooksList hooks, final ClassContainer cc) {
		return hooks.contains("Player") && hooks.get("Player").getOrigName().equals(cc.getName());
	}

	@Override
	public boolean execute(final HooksList hooks, final ClassContainer cc) {
		final ClassHook player = hooks.get("Player");
		
		if (!hooks.contains("Character")) {
		    hooks.add("Character", cc.getSuperName());
		}

		final Iterator<MethodNode> mni = cc.getASM().methods.iterator();
		while (mni.hasNext()) {
		    final MethodNode mn = mni.next();
		    
		    if (mn.desc.matches("\\(IL.*;\\)V")) {
                
		        /*final InstructionFinder<AbstractInsnNode> finder = new InstructionFinder<AbstractInsnNode>(
		                mn, true);
		        
		        final FieldInsnNode fin = (FieldInsnNode) finder.getNext();
		        */
		        System.out.println("hax " + mn.name);
		        break;
            }
		}
		
		for (final FieldNode fn : cc.getASM().fields) {
		    if (fn.desc.equals("I")) {
		        player.add("int" + fn.name, fn);
		    }
		}
		
		return true;
		/*

		final InstructionFinder<AbstractInsnNode> finder = new InstructionFinder<AbstractInsnNode>(
				mn, true);

		IntInsnNode iin = null;
		iin = (IntInsnNode) finder.getNext(InsnCondition.ASM.makeIntCond(BIPUSH, -27));
		if (iin != null) {
			final FieldInsnNode fin = (FieldInsnNode) finder.getPrev(InsnCondition.ASM.GETFIELD_I);
			player.add("visiblePrayer", fin);
			
			return true;
		}
		*/
	}

}